<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卢斌</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="javaScript/iphost.js"></script>
    <script type="text/javascript" src="javaScript/datagrid-filter.js"></script>
</head>
<body>
<script>
    $(function () {
        selnum = [];
        var initurl = ipAddress + "getparts";
        var dg = $('#partrecord').datagrid({
            url:initurl,
            border:false,
            method:'post',
            queryParams:{
                account: localStorage.getItem("userAccount"),
                password: localStorage.getItem("password")
            },
            striped: true,
            singleSelect:false,
            idField: 'partID',
            fit:true,
            fitColumns:true,
            ctrlSelect:false,
            loadMsg:'^_^请稍等',
            emptyMsg: '您还没有审批任何作品',
            pagination:true,
            pageSize: 10000000,
            pageList: [10000000, 25, 10],
            columns:[[
                {
                    field: 'partID',
                    title: '零件编号',
                    width: '10%',
                    align: 'center'
                },
                {
                    field: 'partName',
                    title: '零件名称',
                    width: '19%',
                    align: 'center'
                },
                {
                    field: 'remaining',
                    title: '剩余库存',
                    width: '18%',
                    align: 'center',
                    formatter: function(value,row,index) {
                        if(row.remaining <= row.waring_num){
                            return '<span style="color:red;">('+row.remaining+')</span>';
                        }else{
                            return value;
                        }
                    }
                },
                {
                    field: 'waring_num',
                    title: '警告剩余量',
                    width: '18%',
                    align: 'center'
                },
                {
                    field: 'rmb',
                    title: '单价[元]',
                    width: '18%',
                    align: 'center'
                },
                {
                    field: 'allrmb',
                    title: '零件总价值[元]',
                    width: '18%',
                    align: 'center',
                    formatter: function(value,row,index) {
                        return (row.rmb * row.remaining).toFixed(3);
                    }
                }
            ]]
        });
        dg.datagrid('enableFilter');
        var fbutton = dg.datagrid('getPager');
        fbutton.pagination({
            buttons:[
                {
                    iconCls: 'icon-lock',
                    handler: function() {
                        selectedrowsdata = [];
                        selectedrowsdata = $('#partrecord').datagrid('getSelections');
                        if(selectedrowsdata == null || selectedrowsdata.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg:'请选择添加产品信息的零部件',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else {
                            $.messager.confirm('警告', '确认添加产品分组前，请确认您选择的零件是否正确完整.', function(r){
                                if(r){
                                    selnum = [];
									for(var i = 0;i<selectedrowsdata.length;i++){
									    var tt = selectedrowsdata[i];
									    var row = {
									        name: tt.partID+" "+tt.partName,
									        value:1,
									        editor:'numberbox'
									    };
									    var ts = {};
									    ts.partID = tt.partID;
									    ts.number = 1;
									    selnum.push(ts);
									    $('#dl').propertygrid('appendRow',row);
									}
                                    $('#dl').propertygrid({
                                        showGroup: false,
                                        scrollbarSize: 0,
                                        onAfterEdit:function (index, row, changes) {
                                            if(changes.value<1){
                                                row.value = 1;
                                                $.messager.show({
                                                    title:'提示',
                                                    msg:'请输入有效的数据',
                                                    timeout:1900,
                                                    showType:'slide'
                                                });
                                            }else{
                                                var tname = row.name;
                                                var tsp = tname.split(" ");
                                                for(var j = 0;j<selnum.length;j++){
                                                    if(selnum[j].partID+"" == tsp[0]){
                                                        selnum[j].number = changes.value;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    });
                                    $('#showselectlist').dialog('open');
                                }
                            });
                        }
                    }
                },
                {
                    iconCls: 'icon-clear',
                    handler: function() {
                        $("#partrecord").datagrid('clearSelections');
                    }
                },
                {
                    iconCls: 'icon-add',
                    handler: function() {
                        $('#addPart').dialog('open');
                    }
                },
                {
                    iconCls: 'icon-edit',
                    handler: function() {
                        var selectoneRow = $('#partrecord').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请选择要重命名的零件信息',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else {
                            if(selectoneRow.length != 1){
                                $.messager.show({
                                    title:'提示',
                                    msg: '修改名称只能选择一条数据',
                                    timeout:1900,
                                    showType:'slide'
                                });
                            }else {
                                $.messager.prompt('修改零件名称', '输入新零件名:', function(r){
                                    if (r){
                                        var renameurl = ipAddress + "reName";
                                        $.ajax({
                                            type:'post',
                                            dataType:'json',
                                            url:renameurl,
                                            data:{
                                                account:localStorage.getItem("userAccount"),
                                                password:localStorage.getItem("password"),
                                                partID:selectoneRow[0].partID,
                                                newName:r
                                            },
                                            success:function (returndata) {
                                                $.messager.show({
                                                    title:'提示',
                                                    msg: returndata.message,
                                                    timeout:2500,
                                                    showType:'slide'
                                                });
                                                $('#partrecord').datagrid('reload');
                                                $("#partrecord").datagrid('clearSelections');
                                            },
                                            error:function (e) {
                                                $("#partrecord").datagrid('clearSelections');
                                                alert("请求失败");
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-undo',
                    handler: function() {
                        var selectoneRow = $('#partrecord').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'配件库存扣除',
                                msg: '请选择损失的零件',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else {
                            if(selectoneRow.length > 1){
                                $.messager.show({
                                    title:'配件库存扣除',
                                    msg: '请选择一条配件信息，请勿多选',
                                    timeout:1900,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.prompt('库存信息修改', '请输入该零件损失数量:', function(r) {
                                    if (r) {
                                        if (!/^\d+$/.test(r)) {
                                            alert("请输入正整数");
                                        }else{
                                            var cuturl = ipAddress+"cutOnePartNumber";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url: cuturl,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    partID: selectoneRow[0].partID,
                                                    cutNumber:r
                                                },
                                                success:function (datas) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    $('#partrecord').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: datas.message,
                                                        timeout:2000,
                                                        showType:'slide'
                                                    });

                                                },
                                                error:function (e) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    alert("请求错误");
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-redo',
                    handler: function() {
                        var selectoneRow = $('#partrecord').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请选择入库的零件',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else {
                            if(selectoneRow.length > 1){
                                $.messager.show({
                                    title:'提示',
                                    msg: '入库操作每次只能修改一种配件的库存信息，请勿多选',
                                    timeout:2300,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.prompt('库存信息修改', '请输入该零件入库数量:', function(r) {
                                    if (r) {
                                        if (!/^\d+$/.test(r)) {
                                            alert("请输入正整数");
                                        }else{
                                            var cuturl = ipAddress+"addOnePartNumber";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url: cuturl,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    partID: selectoneRow[0].partID,
                                                    addNumber:r
                                                },
                                                success:function (datas) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    $('#partrecord').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: datas.message,
                                                        timeout:2000,
                                                        showType:'slide'
                                                    });

                                                },
                                                error:function (e) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    alert("请求错误");
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-reload',
                    handler: function() {
                        var selectoneRow = $('#partrecord').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请选择修改单价信息的零件',
                                timeout:2300,
                                showType:'slide'
                            });
                        }else {
                            if(selectoneRow.length > 1){
                                $.messager.show({
                                    title:'提示',
                                    msg: '修改单价一次只能修改一个配件，请勿多选',
                                    timeout:2300,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.prompt('单价信息修改', '请输入选中零件新单价金额[元]:', function(r) {
                                    if(r){
                                        if(!isNaN(r)){
                                            var resetRmbrul = ipAddress + "resetRmb";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url:resetRmbrul,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    partID: selectoneRow[0].partID,
                                                    newRmb:r
                                                },
                                                success:function (data) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    $('#partrecord').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: data.message,
                                                        timeout:2300,
                                                        showType:'slide'
                                                    });
                                                },
                                                error:function (d) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    alert("请求错误");
                                                }
                                            });
                                        }else{
                                            $.messager.show({
                                                title:'提示',
                                                msg: '请输入有效数值',
                                                timeout:2300,
                                                showType:'slide'
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-sum',
                    handler: function() {
                        var tss = $('#partrecord').datagrid('getData');
                        var inflist = tss.rows;
                        var sumvalue = 0.0;
                        var countnumber = 0;
                        for(var ii = 0;ii<inflist.length;ii++){
                            var ttsp = inflist[ii];
                            countnumber = countnumber + 1;
                            sumvalue = sumvalue + (ttsp.remaining * ttsp.rmb);
                        }
                        $.messager.alert('库存金额', "以上配件共计"+countnumber+"种，库存总金额为"+sumvalue+"元",'info');
                    }
                },
                {
                    iconCls: 'icon-tip',
                    handler: function() {
                        var clickrow = $('#partrecord').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'修改产品警戒值',
                                msg: '请先选择要修改警戒值的配件',
                                timeout:2000,
                                showType:'slide'
                            });
                        }else{
                            if(clickrow.length != 1){
                                $.messager.show({
                                    title:'提示',
                                    msg: '请选择一条数据修改',
                                    timeout:2000,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.prompt('警戒值修改', '请输入该零件新警戒值:', function(r) {
                                    if (r) {
                                        if (!/^\d+$/.test(r)) {
                                            alert("请输入正整数");
                                        }else{
                                            var cuturl = ipAddress+"reSetWaring";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url: cuturl,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    partID: clickrow[0].partID,
                                                    waringValue:r
                                                },
                                                success:function (datas) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    $('#partrecord').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: datas.message,
                                                        timeout:2000,
                                                        showType:'slide'
                                                    });
                                                },
                                                error:function (e) {
                                                    $("#partrecord").datagrid('clearSelections');
                                                    alert("请求错误");
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-cut',
                    handler: function() {
                        var selectoneRow = $('#partrecord').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请选择要删除的零件信息',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else{
                            if(selectoneRow.length != 1){
                                $.messager.show({
                                    title:'提示',
                                    msg:'每次只能删除一条配件信息',
                                    timeout:1900,
                                    showType:'slide'
                                });
                            }else{
                                var deleterul = ipAddress + "deleteOnePart";
                                $.messager.confirm('警告', '确定删除？', function(r){
                                    if(r){
                                        $.ajax({
                                            type:'post',
                                            dataType:'json',
                                            url:deleterul,
                                            data:{
                                                account:localStorage.getItem("userAccount"),
                                                password:localStorage.getItem("password"),
                                                partID: selectoneRow[0].partID
                                            },
                                            success:function (returndata) {
                                                $.messager.show({
                                                    title:'提示',
                                                    msg: returndata.message,
                                                    timeout:2500,
                                                    showType:'slide'
                                                });
                                                $('#partrecord').datagrid('reload');
                                                $("#partrecord").datagrid('clearSelections');
                                            },
                                            error:function (e) {
                                                $("#partrecord").datagrid('clearSelections');
                                                alert("请求失败");
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    }
                }
            ]
        });
    });
</script>
<table id="partrecord" class="easyui-datagrid"></table>
<div id="showselectlist" class="easyui-dialog" title="确认选择零件" data-options="iconCls:'icon-save',closed:true"
     style="width:400px;height:420px;padding:10px;text-align: center">

    <table id="dl" class="easyui-propertygrid"></table>
    </br>
    <div style="margin-bottom:20px">
        <input id="inputmachineName" class="easyui-textbox" name="schid" prompt="20字以内:" style="width:100%" data-options="label:'产品型号:',required:true,iconCls:'icon-tip'">
    </div>
    <a id="submitform" href="javascript:void(0)" class="easyui-linkbutton" onclick="acceptchoose()" style="width:30%">提交</a>
</div>
<div id="addPart" class="easyui-dialog" title="零件信息" data-options="iconCls:'icon-save',closed:true"
     style="width:400px;height:420px;padding:10px;text-align: center">
    <form id="ff">
        <div style="margin-bottom:20px">
            <input id="inputpartName" class="easyui-textbox" name="schid" prompt="20字以内:" style="width:100%" data-options="label:'零件名:',required:true">
        </div>
        <div style="margin-bottom:20px">
            <input id="inputNumber" class="easyui-numberbox" name="schid" prompt="0-999999999:" style="width:100%" data-options="label:'库存量:',required:true, min: 0, max: 999999999, precision:0">
        </div>
        <div style="margin-bottom:20px">
            <input id="inputlimitNumber" class="easyui-numberbox" name="schid" prompt="1-999999999:" style="width:100%" data-options="label:'警告数量:',required:true, min: 1, max: 999999999, precision:0">
        </div>
        <div style="margin-bottom:20px">
            <input id="rbm" class="easyui-numberbox" prompt="0-9999999:" style="width:100%" data-options="label:'零件单价:',required:true, min:0, max:999999999,precision:5">
        </div>
        <a id="submitpart" href="javascript:void(0)" class="easyui-linkbutton" onclick="addpart()" style="width:30%">提交</a>
    </form>
</div>
<script>
    function addpart() {
        $('#submitpart').linkbutton('disable');
        var addparturl = ipAddress + "addOnePart";
        var partnamevalue = document.getElementById("inputpartName").value;
        var partremaivalue = document.getElementById("inputNumber").value;
        var limitvalue = document.getElementById("inputlimitNumber").value;
        var danjia = document.getElementById("rbm").value;
        if(partnamevalue == null || partremaivalue == null || limitvalue == null || danjia == null){
            $.messager.show({
                title:'提示',
                msg: '请输入完整有效的零件信息',
                timeout:1900,
                showType:'slide'
            });
        }else{
            $.ajax({
                type:'post',
                dataType: 'json',
                url:addparturl,
                data:{
                    account:localStorage.getItem("userAccount"),
                    password:localStorage.getItem("password"),
                    partName: partnamevalue,
                    remainNumber: partremaivalue,
                    limitNumber: limitvalue,
                    rmb: danjia
                },
                success:function (returndata) {
                    $.messager.show({
                        title:'提示',
                        msg: returndata.message,
                        timeout:1900,
                        showType:'slide'
                    });
                    $('#partrecord').datagrid('reload');
                    $('#ff').form('clear');
                    $('#submitpart').linkbutton('enable');
                },
                error:function (e) {
                    alert("请求错误");
                    $('#ff').form('clear');
                    $('#submitpart').linkbutton('enable');
                }
            })
        }
    }
    function acceptchoose() {
        var namevalue = document.getElementById("inputmachineName").value;
        var partMap = "";
        if(namevalue == null || namevalue.trim().length == 0 || namevalue.trim().length>20){
            $.messager.show({
                title:'提示',
                msg:'请输入有效产品型号名称',
                timeout:1900,
                showType:'slide'
            });
        }else{
            var keyi = 1;
            for(var i = 0;i<selnum.length;i++){
                var tsps = selnum[i];
                if(tsps.number<1){
                    keyi = 0;
                    break;
                }
                partMap = partMap + tsps.partID + " "+tsps.number + " ";
            }
            if(keyi == 0){
                $.messager.show({
                    title:'提示',
                    msg:'您输入的数量信息有误，请重新检查',
                    timeout:1900,
                    showType:'slide'
                });
            }else{
                var urladdp = ipAddress + "addOneMachine";
                var postpara = {};
                postpara.account = parseInt(localStorage.getItem("userAccount"));
                postpara.password = localStorage.getItem("password");
                postpara.name = namevalue;
                postpara.infor = partMap;
                $.ajax({
                    type: 'post',
                    dataType:'json',
                    url:urladdp,
                    contentType : 'application/json',
                    data:JSON.stringify(postpara),
                    success:function (retuendata) {
                        $.messager.confirm('提示', retuendata.message, function(r){
                            location.reload();
                        });
                    },
                    error:function (e) {
                        alert("请求失败");
                        location.reload();
                    }
                })
            }
        }
    }
</script>
</body>
</html>